package com.sk.aop.proxy;

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CGLibMethodInterceptor implements MethodInterceptor {//2. 实现接口
    private Object target;//4. 代理对象

    public CGLibMethodInterceptor(Object target) {
        this.target = target;
    }

    /**
     * 调用代理对象的方法
     * @param obj
     * @param method
     * @param args
     * @param proxy
     * @return
     * @throws Throwable
     */
    @Override//3. 重写方法
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        //用Method或者proxy都可以
        System.out.println("我是中介，开始代理");
        Object result = method.invoke(target, args);
        System.out.println("我是中介，结束代理");
        return result;
    }
}
